package com.janyee.buddha.adapter

import android.graphics.Color
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView

import com.janyee.buddha.R
import com.janyee.buddha.adapter.IBookFliperAdapter
import com.janyee.buddha.view.TestText

import java.util.ArrayList

/**
 * Created by kmlixh on 17/12/21.
 */

class TestAdapter : IBookFliperAdapter {
    override fun pagePreviousPre() {

    }

    lateinit var l1: TestText
    lateinit var l2: TestText
    lateinit var l3: TestText
    var images: MutableList<TestText> = ArrayList()
    private var viewGroup: ViewGroup? = null

    override fun init(viewGroup: ViewGroup) {
        l1 = TestText(viewGroup.context)
        l1.setBackgroundColor(Color.YELLOW)
        val lp1 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        l2 = TestText(viewGroup.context)
        l2.setBackgroundColor(Color.RED)

        val lp2 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        lp2.leftMargin = 0
        l3 = TestText(viewGroup.context)
        l3.setBackgroundColor(Color.BLUE)
        val lp3 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        lp3.leftMargin = -viewGroup.width
        Log.e("====", "lp3.margin" + lp3.leftMargin)
        l1.setBackgroundResource(R.mipmap.test)
        l2.setBackgroundResource(R.mipmap.test2)
        l3.setBackgroundResource(R.mipmap.test)
        l1.text = "l1"
        l2.text = "l2"
        l3.text = "l3"
        l1.setTextColor(Color.WHITE)
        l2.setTextColor(Color.WHITE)
        l3.setTextColor(Color.WHITE)
        l1.gravity = Gravity.CENTER
        l2.gravity = Gravity.CENTER
        l3.gravity = Gravity.CENTER
        //        l2.setOnLongClickListener(new View.OnLongClickListener() {
        //            @Override
        //            public boolean onLongClick(View v) {
        //                Log.e("=====","long clicked");
        //                return true;
        //            }
        //        });
        l1.layoutParams = lp1
        l2.layoutParams = lp2
        l3.layoutParams = lp3
        images.add(l1)
        images.add(l2)
        images.add(l3)
        viewGroup.addView(l1)
        viewGroup.addView(l2)
        viewGroup.addView(l3)
        this.viewGroup = viewGroup

    }

    override operator fun hasNext(): Boolean {
        return true
    }

    override fun hasPrevious(): Boolean {
        return true
    }

    override operator fun next(): View {
        return l3
    }

    override fun previous(): View {
        return l1
    }

    override fun current(): View {
        return l2
    }

    override fun pagePrevious() {
        val t = l1.text.toString()
        l1.text = l2.text
        l2.text = l3.text
        l3.text = t
        l1.invalidate()
        l2.invalidate()
        l3.invalidate()
    }

    override fun pageNext() {

        val t = l3.text.toString()
        l3.text = l2.text
        l2.text = l1.text
        l1.text = t
        l1.invalidate()
        l2.invalidate()
        l3.invalidate()
    }

    override fun callLongClick() {

    }

    override fun cancelLongClick() {

    }
}
